@extends("layout/admin")
@section("css")
    <link href="/admins/css/plugins/dataTables/datatables.min.css" rel="stylesheet">
@endsection
@section("content")
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>Data Tables</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="index.html">Home</a>
                </li>
                <li>
                    <a>Tables</a>
                </li>
                <li class="active">
                    <strong>Data Tables</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>Basic Data Tables example with responsive plugin</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="table_data_tables.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="table_data_tables.html#">Config option 1</a>
                                </li>
                                <li><a href="table_data_tables.html#">Config option 2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">

                        <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover dataTables-example" >
                                <thead>
                                <tr>
                                    <th>用户</th>
                                    <th>商品</th>
                                    <th>评价内容</th>
                                    <th>回复内容</th>
                                    <th>是否生效</th>
                                    <th>评论时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($commentInfo as $k => $v)
                                    <tr class="gradeX">
                                        <td>{{$v->user->username}}</td>
                                        <td>{{$v->goods->name}}</td>
                                        <td>{{$v['content']}}</td>
                                        <td>@if($v['reply_status']==1) {{$v['reply']}} @else 未回复 @endif</td>
                                        <td class="center">
                                            <div class="switch">
                                                <div class="onoffswitch">
                                                    <input type="checkbox" @if($v->status == 1) checked @endif class="onoffswitch-checkbox" id="example1">
                                                    <label class="onoffswitch-label" data="{{$v->id}}" onclick="changeCommentStatus($(this))" for="example1">
                                                        <span class="onoffswitch-inner"></span>
                                                        <span class="onoffswitch-switch"></span>
                                                    </label>
                                                </div>
                                            </div>
                                        </td>
                                        <td>{{$v['created_at']}}</td>
                                        <td>
                                            @if($v->reply_status==1)
                                                <button class="btn btn-primary btn-sm" disabled="disabled"><i class="fa fa-eye"></i> 已回复</button>
                                            @else
                                                <button class="btn btn-white btn-sm" data-toggle="modal" data-target="#myModal" onclick="reply($(this))" data="{{$v->id}}"><i class="fa fa-eye"></i> 回复</button>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                                <tfoot>
                                <tr>
                                    <th>用户</th>
                                    <th>商品</th>
                                    <th>评价内容</th>
                                    <th>回复状态</th>
                                    <th>是否生效</th>
                                    <th>评论时间</th>
                                    <th>操作</th>
                                </tr>
                                </tfoot>
                            </table>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        回复内容
                    </h4>
                </div>
                <div class="modal-body">
                    <form method="post" class="form-horizontal reply-form">
                        <input type="hidden" name="id" value="" />
                        <div class="form-group">
                            <div class="col-sm-12"><textarea class="form-control message-input" name="info" placeholder="请填写回复内容"></textarea></div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消
                    </button>
                    <button type="button" class="btn btn-primary submit">
                        确定
                    </button>
                </div>
            </div>
        </div>
    </div>
@endsection
@section("js")
    <script src="/admins/js/plugins/dataTables/datatables.min.js"></script>
    <script src="/admins/js/plugins/jeditable/jquery.jeditable.js"></script>
    <script>
        $(document).ready(function(){
            $('.dataTables-example').DataTable({
                dom: '<"html5buttons"B>lTfgitp',
                buttons: [
                    { extend: 'copy'},
                    {extend: 'csv'},
                    {extend: 'excel', title: 'ExampleFile'},
                    {extend: 'pdf', title: 'ExampleFile'},

                    {extend: 'print',
                        customize: function (win){
                            $(win.document.body).addClass('white-bg');
                            $(win.document.body).css('font-size', '10px');

                            $(win.document.body).find('table')
                                    .addClass('compact')
                                    .css('font-size', 'inherit');
                        }
                    }
                ]

            });
            /* Init DataTables */
            var oTable = $('#editable').DataTable();
            /* Apply the jEditable handlers to the table */
            oTable.$('td').editable( '../example_ajax.php', {
                "callback": function( sValue, y ) {
                    var aPos = oTable.fnGetPosition( this );
                    oTable.fnUpdate( sValue, aPos[0], aPos[1] );
                },
                "submitdata": function ( value, settings ) {
                    return {
                        "row_id": this.parentNode.getAttribute('id'),
                        "column": oTable.fnGetPosition( this )[2]
                    };
                },

                "width": "90%",
                "height": "100%"
            } );
            /* Init DataTables */
            var oTable = $('#editable').DataTable();
            $(".submit").on("click", function(){
                id = $(this).parents(".modal-content").find("input[name='id']").val();
                message = $(this).parents(".modal-content").find("textarea[name='info']").val();
                info = "回复成功！";
                $.get("/admin/comment-reply",{"id":id,"message":message},function(data){
                    if(data == 1){
                        $('#myModal').modal('hide')
                        $("tr[data='" + id + "']").remove();
                        toastr.options = {
                            showDuration: "300",
                            hideDuration: "300",
                            timeOut: "1000",
                        };
                        toastr.success(info);
                    }
                });
            });


        });

        function fnClickAddRow() {
            $('#editable').dataTable().fnAddData( [
                "Custom row",
                "New row",
                "New row",
                "New row",
                "New row" ] );

        }
        function reply(obj){
            var id = obj.attr("data");
            $(".reply-form").find("input[name='id']").val(id);
        }
        function changeCommentStatus(obj){
            var comment_id = obj.attr("data");
            var status = 0;
            info = "";
            if(obj.siblings("input[type='checkbox']").is(':checked')){
                status = 0;
                info = "评论不生效！"
            }else{
                status = 1;
                info = "评论生效！";
            }
            $.get("/admin/changeCommentStatus",{"comment_id":comment_id,"status":status},function(data){
                if(data == 1){
                    toastr.options = {
                        showDuration: "300",
                        hideDuration: "300",
                        timeOut: "1000",
                    };
                    toastr.success(info);
                }
            });
        };

    </script>
@endsection